package com.restaurant.ressys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.restaurant.ressys.entity.User;
import com.restaurant.ressys.service.UserService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangsan
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @GetMapping("/login/{userName}/{pwd}/{role}")
    public boolean login(@PathVariable("userName") String userName, @PathVariable("pwd") String pwd,@PathVariable("role")int role, HttpSession session){

        if(role==1){
            QueryWrapper<User> wrapper=new QueryWrapper<>();
            wrapper.eq("name",userName).eq("pwd",pwd);
            User user=userService.getOne(wrapper);
            session.setAttribute("user",user);
            return true;
        }
        if (role==2){
            if("admin".equals(userName)&&"admin".equals(pwd)){
                return true;
            }
        }
        return false;
    }
    @GetMapping("/getAll")
    public List<User> getAll(){
        return  userService.list();
    }
    @PostMapping("/add")
    public void add(@RequestBody User user){
        userService.save(user);
    }
    @DeleteMapping("/del/{id}")
    public void del(@PathVariable Integer id){
        userService.removeById(id);
    }

}

